package com.gm.wholesale;

import android.content.Context;

import com.gm.commonlib.application.ActivityLifecycleCallbacksImpl;
import com.gm.commonlib.application.CommonBaseApplication;
import com.gm.commonlib.application.MyApplicationHolder;
import com.gm.commonlib.network.BaseHttpClientManger;
import com.gm.commonlib.network.HttpResultCode;
import com.gm.commonlib.network.basenetwork.config.HttpOptions;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.constant.Constant;
import com.gm.wholesale.db.DatabaseHelper;
import com.gm.wholesale.module.wx.share.WXShareUtil;
import com.gm.wholesale.network.RetrofitHeaderInterceptor;
import com.gm.wholesale.util.AppUtil;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.taobao.sophix.SophixManager;
import com.tencent.bugly.crashreport.CrashReport;


public class GMApplication extends CommonBaseApplication implements MyApplicationHolder.MyApplicationInterface {
    //ormlite数据库
    private DatabaseHelper mDatabaseHelper;
    @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacksImpl());
        //中间桥梁
        MyApplicationHolder.init(this);
        //网络
        initNetWork();
        //初始化数据库
        initDB();
        //初始化语音识别
        initVoice();
        //腾讯bugly
        initTenBugly();
        //微信分享
        initWx();

    }

    private void initWx(){ //微信分享
        WXShareUtil.initWx(this);
    }

    //腾讯Bugly
    private void initTenBugly(){
        CrashReport.initCrashReport(getApplicationContext());
    }


    //科大讯飞语音识别
    private void initVoice(){
       SpeechUtility.createUtility(getContext(), SpeechConstant.APPID +"="+Constant.keda_appId);
    }

    private void initDB(){
        mDatabaseHelper = new DatabaseHelper(this);

    }
    public DatabaseHelper getDatabaseHelper() {
        if (mDatabaseHelper==null){
            initDB();
        }
        return mDatabaseHelper;
    }


    @Override
    public Context getContext() {
        return this;
    }

    //接口地址
    @Override
    public String getBaseUrl() {
        String host = BuildConfig.URL_HOST;
        return host;
    }

    //todo token失效后会走这里 重新登录
    @Override
    public void login(int code) {
        ToastUtil.showToast(HttpResultCode.TOKEN_ERROR.getMsg());
        AppUtil.startLoginAct(getContext());

    }


    // todo 初始化网络框架
    public void initNetWork() {
        HttpOptions.Builder options = new HttpOptions.Builder();
        options.setInterceptor(new RetrofitHeaderInterceptor());
        BaseHttpClientManger.getBaseHttpClientManger().initHttpClient(options);
    }
}
